import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Index from '@/components/Index'
import UserInfo from '@/components/user/UserInfo'
import Charts from "@/components/Charts";
import Pie from "@/components/Pie"
import Table from "@/components/Table"
import Real from "@/components/Real"
import UpdatePassword from "@/components/user/UpdatePassword";
import PanelGroup from "@/components/PanelGroup";
import Today from "@/components/Today";
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'login',
      component: Login
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/index',
      name: 'Index',
      component: Index,
      redirect: "/charts",
      children:[
        { path: '/user/userInfo', name: 'UserInfo', component: UserInfo },
        { path: '/charts', name: 'Charts', component: Charts },
        { path: '/pie', name: 'Pie', component: Pie },
        { path: '/table', name: 'Table', component: Table },
        { path: '/real', name: 'Real', component: Real },
        { path: '/panelGroup', name: 'PanelGroup', component: PanelGroup },
        { path: '/today', name: 'Today', component: Today },
        { path: '/user/updatePassword', name: 'UpdatePassword', component: UpdatePassword }
      ]
    }
  ]
})

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  } else {
    let token = localStorage.getItem('Authorization');
    if (token === null || token === '') {
      console.log("无权限")
      next('/login');
    } else {
      console.log("有权限")
      next();
    }
  }
});

export default router;
